<?php

class CartController extends CommonController {

	public function ajax_add_cart() {
		if (IS_POST) {
			$data = K('Glist') -> get_one_data(array('apid' => Q('post.apid')));
			//			p($_POST);
			foreach ($data as $k => $v) {
				$data['gpvalue'] = explode(',', $data['apid']);
				$data['good'] = K('Good') -> get_one_data(array('gid' => $data['gid']));
				foreach ($data['gpvalue'] as $key => $value) {
					$data['gpvalue'][$key] = K('Gproperty') -> where(array('gpid' => $value)) -> find();

				}
			}

			foreach ($data['gpvalue'] as $key => $value) {
				$data['gpvalue'][$key]['tpname'] = K('Tproperty') -> where(array('tpid' => $value['tpid'])) -> getField('tpname');

			}
			foreach ($data['gpvalue'] as $key => $value) {
				$data['gpvalue'][$key] = $value['tpname'] . ':' . $value['gpvalue'];

			}

			//			p(q('post.num'));

//			p($data);

			$sub_total = $data['good']['price'] * Q('post.num');
			$cart = array('id' => $data['gid'], 'name' => $data['good']['gname'], 'num' => Q('post.num'), 'price' => $data['good']['price'], 'mprice' => $data['good']['mprice'], 'lthubm' => $data['good']['lthumb'], 'sub_total' => $sub_total, 'apid' => $data['apid'], 'options' => array('size' => $data['gpvalue'][0], 'color' => $data['gpvalue'][1], ), );
			cart::add($cart);
			$this -> success('添加成功', U("ajax_add_cart"));
		}


		$this -> dis('cart');

	}

	public function ajax_update_cart() {

		if (!IS_AJAX) $this -> error('非法请求');
		//		  echo Q('post.r');

		$arr = explode('|', Q('post.r'));
		$num = Q('post.num');
		$price = $_SESSION['cart']['goods'][$arr[0]]['price'];
		$t = $_SESSION['cart']['total'];
		$i = K('Glist') -> where(array('gid' => $arr[1], 'apid' => $arr[2])) -> getField('glinventory');
		if ($i - $num >= 0 && $num>=0) {
			$data = array('sid' => $arr[0], 'num' => $num,);
			Cart::update($data);
			$data['p']=$price * $num;
			$data['t']=$_SESSION['cart']['total'];
			//			  p($data);
			//			unset($_SESSION['cart']['goods']['5216db7c']);
			$data['status'] = true;
		} else {
			$data['status'] = false;
		}

		$this -> ajax($data);
	}

	//		  p($i);
	
	public function del(){
		
		$sid=Q('get.sid');
		Cart::del($sid);
		$this->success('删除成功');
	}
	
	public function del_all(){
		$sid=Q('get.sid');
		if(!$sid) $this->error('请选择需删除的商品');
		$sid=explode(',', $sid);
		foreach ($sid as $k => $v) {
			Cart::del($v);
		}
		$this->success('删除成功');
	}
	
	public function settle(){
		
		if(!$_SESSION['cart']['goods']) $this->error('请先选择商品',U('Index/index'));
		
		$address=K('Address')->get_all_data(array('uid'=>$_SESSION['uid']),'defau DESC');
		
//		p($address);
		$this->assign('address',$address);
		$sid=explode(',', Q('get.sid'));
//		p($_SESSION);
		
		$arr=$_SESSION['cart']['goods'];
		$all=array_keys($arr);
//		p($all);
		$a=array_diff($all,$sid);
		
//		p($a);
		$order=array();
		foreach ($sid as $k => $v) {
			foreach ($arr as $key => $value) {
				if($v==$key){
					$order[]=$value;
				}
			}
		}
		$data=array();
		$data['total']=0;
		$data['num']=0;
		foreach ($order as $k => $v) {
			$data['total'] +=$v['total'];
			$data['num'] +=$v['num'];
		}
//		p($order);die;
		$this->assign('data',$data);
		$this->assign('order',$order);
		$this->dis('settle');
	}
	
	public function ajax_settle(){
		if(!IS_AJAX) $this->error('非法请求');
		$sid=Q('post.sid');
//		p($_GET);
		$data=array();
		if($sid){
			$data=array(
			'sid'=>$sid,
		);
			$data['status'] = true;
		}else{
			$data['status'] = false;
		}
		$this -> ajax($data);
//		p($data);
	}
	

}











